package com.ruibang.glass.material.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruibang.glass.material.constant.MaterialWareHouseCode;
import com.ruibang.glass.material.domain.req.ApproveProcessReq;
import com.ruibang.glass.material.domain.req.MaterialALLInventoryManagementReq;
import com.ruibang.glass.material.domain.req.ycl.MaterialInventoryReq;
import com.ruibang.glass.material.entity.MaterialALLInventoryManagement;
import com.ruibang.glass.material.entity.MaterialALLOutboundManagement;
import com.ruibang.glass.material.entity.MaterialLineSideWarehouseLocation;
import com.ruibang.glass.material.service.GMaterialALLInventoryManagementService;
import com.ruibang.glass.material.service.MaterialLineSideWarehouseLocationService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 全模块库存管理表 前端控制器
 * </p>
 *
 * @author mameng
 * @since 2024-02-05
 */
@RestController
@RequestMapping("/api/material/all/inventory/management")
@Api(value = "全模块库存管理表", tags = {"全模块库存管理表"})
public class MaterialALLInventoryManagementController {


    @Autowired
    private GMaterialALLInventoryManagementService materialInventoryManagementService;
    @Autowired
    private MaterialLineSideWarehouseLocationService materialLineSideWarehouseLocationService;

    @PostMapping("/list")
    @ApiOperation(value = "全模块库存管理表", response = ResultBody.class)
    public ResultBody getPageList(@RequestBody MaterialInventoryReq materialManagementReq) {
        return ResultBody.success(materialInventoryManagementService.getPage(materialManagementReq));
    }


    @PostMapping("check/list")
    @ApiOperation(value = "盘点使用查询", response = ResultBody.class)
    public ResultBody getCheckPageList(@RequestBody MaterialInventoryReq materialManagementReq) {
        return ResultBody.success(materialInventoryManagementService.getCheckPageList(materialManagementReq));
    }

    @GetMapping("/getSelectWareHouse")
    @ApiOperation(value = "库存仓库表", response = ResultBody.class)
    public ResultBody getSelectWareHouse() {
        return ResultBody.success(materialInventoryManagementService
                .list(Wrappers.<MaterialALLInventoryManagement>lambdaQuery()
                        .select(MaterialALLInventoryManagement::getId, MaterialALLInventoryManagement::getWareHouseName)));
    }

    @GetMapping("/getMaterialByName/{name}")
    @ApiOperation(value = "库存仓库表", response = ResultBody.class)
    public ResultBody getMaterialByName(@PathVariable("name") String name) {
        return ResultBody.success(materialInventoryManagementService
                .list(Wrappers.<MaterialALLInventoryManagement>lambdaQuery()
                        .select(MaterialALLInventoryManagement::getMaterialId, MaterialALLInventoryManagement::getMaterialName, MaterialALLInventoryManagement::getMaterialCode).eq(MaterialALLInventoryManagement::getWareHouseName, name)
                ));
    }

    @GetMapping("/getMaterialSelectList")
    @ApiOperation(value = "库存物料下拉", response = ResultBody.class)
    public ResultBody getMaterialSelectList() {
        return ResultBody.success(materialInventoryManagementService
                .list(Wrappers.<MaterialALLInventoryManagement>lambdaQuery()
                        .select(MaterialALLInventoryManagement::getMaterialId,
                                MaterialALLInventoryManagement::getUnit,
                                MaterialALLInventoryManagement::getStorageLocation,
                                MaterialALLInventoryManagement::getSupplier,
                                MaterialALLInventoryManagement::getSpecification,
                                MaterialALLInventoryManagement::getMaterialName,
                                MaterialALLInventoryManagement::getMaterialCode,
                                MaterialALLInventoryManagement::getWareHouseCode,
                                MaterialALLInventoryManagement::getBarcodeNumber,
                                MaterialALLInventoryManagement::getWareHouseId,
                                MaterialALLInventoryManagement::getStockQuantity,
                                MaterialALLInventoryManagement::getAvailableQuantity,
                                MaterialALLInventoryManagement::getBatchNumber)
                        .ge(MaterialALLInventoryManagement::getStockQuantity, Constants.ONE)
                        .ne(MaterialALLInventoryManagement::getWareHouseCode, MaterialWareHouseCode.BCPK_CK.getMsg())
                        .ne(MaterialALLInventoryManagement::getWareHouseCode, MaterialWareHouseCode.CPK_CK.getMsg())
                        .ne(MaterialALLInventoryManagement::getWareHouseCode, MaterialWareHouseCode.XBC_CK.getMsg())
                        .ne(MaterialALLInventoryManagement::getWareHouseCode, MaterialWareHouseCode.YCLK_CK.getMsg())
                ));
    }

    @GetMapping("/getMaterialLocationSelectList")
    @ApiOperation(value = "物料去重下拉", response = ResultBody.class)
    public ResultBody getMaterialLocationSelectList() {
        return ResultBody.success(materialInventoryManagementService.getMaterialLocationSelectList());
    }

    @GetMapping("/getMaterialLocationByItemCode/{itemCode}")
    @ApiOperation(value = "物料去重下拉", response = ResultBody.class)
    public ResultBody getMaterialLocationByItemCode(@PathVariable("itemCode") String itemCode) {
        return ResultBody.success(materialInventoryManagementService.getMaterialLocationByItemCode(itemCode));
    }


    @GetMapping("/yclSelectList")
    @ApiOperation(value = "原材料下拉", response = ResultBody.class)
    public ResultBody yclSelectList() {
        return ResultBody.success(materialInventoryManagementService.
                getMaterialALLInventoryManagementDTO()
        );
    }

    @GetMapping("/getYclMaterialList")
    @ApiOperation(value = "原材料物料信息", response = ResultBody.class)
    public ResultBody getYclMaterialList() {
        return ResultBody.success(materialInventoryManagementService.getYclMaterialList()
        );
    }

    @GetMapping("/getYclLocationMaterial")
    @ApiOperation(value = "原材料下拉", response = ResultBody.class)
    public ResultBody getYclLocationMaterial(String barcodeNumber, String batchNumber) {
        return ResultBody.success(materialInventoryManagementService.getYclLocationMaterial(barcodeNumber, batchNumber)
        );
    }


    @GetMapping("/pcSelectList/{itemCode}")
    @ApiOperation(value = "批次下拉", response = ResultBody.class)
    public ResultBody pcSelectList(@PathVariable String itemCode) {
        return ResultBody.success(materialInventoryManagementService.
                list(Wrappers.<MaterialALLInventoryManagement>lambdaQuery().select(MaterialALLInventoryManagement::getId, MaterialALLInventoryManagement::getBatchNumber,
                        MaterialALLInventoryManagement::getModelGrade,
                        MaterialALLInventoryManagement::getUnit,
                        MaterialALLInventoryManagement::getStockQuantity,
                        MaterialALLInventoryManagement::getATypeShelf,
                        MaterialALLInventoryManagement::getWareHouseId
                        )
                                .ge(MaterialALLInventoryManagement::getStockQuantity, Constants.ONE)
                                .eq(MaterialALLInventoryManagement::getMaterialCode, itemCode)
                                .isNull(MaterialALLInventoryManagement::getFrozenQuantity)
                )
        );
    }


    @GetMapping("/pcSelectListByPCNumber/{pcNumber}")
    @ApiOperation(value = "批次下拉", response = ResultBody.class)
    public ResultBody pcSelectListByPCNumber(@PathVariable String pcNumber) {
        return ResultBody.success(materialInventoryManagementService.getMaterialALLInventoryManagementByPcNumber(pcNumber));
    }

    @GetMapping("/saleCpSelectList")
    @ApiOperation(value = "成品新增批次下拉", response = ResultBody.class)
    public ResultBody pcSelectList() {
        List<MaterialALLInventoryManagement> list = materialInventoryManagementService.
                list(Wrappers.<MaterialALLInventoryManagement>lambdaQuery().select(
                        MaterialALLInventoryManagement::getId,
                        MaterialALLInventoryManagement::getBatchNumber,
                        MaterialALLInventoryManagement::getATypeShelf,
                        MaterialALLInventoryManagement::getModelGrade,
                        MaterialALLInventoryManagement::getUnit,
                        MaterialALLInventoryManagement::getStorageLocation,
                        MaterialALLInventoryManagement::getMaterialName,
                        MaterialALLInventoryManagement::getMaterialCode,
                        MaterialALLInventoryManagement::getMaterialId,
                        MaterialALLInventoryManagement::getWareHouseId,
                        MaterialALLInventoryManagement::getWareHouseName,
                        MaterialALLInventoryManagement::getStockQuantity
                        )
                                .eq(MaterialALLInventoryManagement::getWareHouseCode, MaterialWareHouseCode.CPK_CK.getMsg())
                                .ge(MaterialALLInventoryManagement::getStockQuantity, Constants.ONE)
                                .isNull(MaterialALLInventoryManagement::getFrozenQuantity)
                                .isNotNull(MaterialALLInventoryManagement::getStorageLocation)
                );
        Map<String, String> exMap = materialInventoryManagementService.getExMap();
        List<String> collect = list.stream().map(MaterialALLInventoryManagement::getStorageLocation).distinct().collect(Collectors.toList());
        if (CollectionUtils.isEmpty(collect)) {
            return ResultBody.success();
        }
        List<MaterialLineSideWarehouseLocation> list1 = materialLineSideWarehouseLocationService.list(Wrappers.<MaterialLineSideWarehouseLocation>lambdaQuery().select(MaterialLineSideWarehouseLocation::getLocationName, MaterialLineSideWarehouseLocation::getId)
                .in(MaterialLineSideWarehouseLocation::getId, collect));
        Map<String, String> locationMap = new HashMap<>();
        for (MaterialLineSideWarehouseLocation materialLineSideWarehouseLocation : list1) {
            locationMap.put(materialLineSideWarehouseLocation.getId(), materialLineSideWarehouseLocation.getLocationName());
        }

        for (MaterialALLInventoryManagement materialALLInventoryManagement : list) {
            if (locationMap != null) {
                if (locationMap.get(materialALLInventoryManagement.getStorageLocation()) != null) {
                    materialALLInventoryManagement.setStorageLocationName(locationMap.get(materialALLInventoryManagement.getStorageLocation()));
                }
            }
            if (exMap != null) {
                if (exMap.get(materialALLInventoryManagement.getMaterialCode()) != null) {
                    materialALLInventoryManagement.setExecutionStandard(exMap.get(materialALLInventoryManagement.getMaterialCode()));
                }
            }
        }

        return ResultBody.success(list);
    }

    @PostMapping("/approveProcess")
    @ApiOperation(value = "审批流程", response = ResultBody.class)
    public ResultBody approveProcess(@RequestBody ApproveProcessReq processReq) {
        return ResultBody.success(materialInventoryManagementService.approveProcess(processReq));
    }


    /**
     * @return
     */
    @GetMapping("/newPcSelectList")
    @ApiOperation(value = "获取最新的批次号和托盘号", response = ResultBody.class)
    public ResultBody newPcSelectList() {
        return ResultBody.success(materialInventoryManagementService.newPcSelectList());
    }

    /**
     * @return
     */
    @GetMapping("/getNewPcNumberList/{aTypeCode}")
    @ApiOperation(value = "获取最新的批次号和托盘号", response = ResultBody.class)
    public ResultBody getNewPcNumberList(@PathVariable("aTypeCode") String aTypeCode) {
        return ResultBody.success(materialInventoryManagementService.getNewPcNumberList(aTypeCode));
    }


    @PostMapping("/updateFrozenQuantity")
    @ApiOperation(value = "原材料领料数量冻结", response = ResultBody.class)
    public ResultBody updateFrozenQuantity(@RequestBody List<MaterialALLInventoryManagementReq> materialALLInventoryManagementReqList) {
        materialInventoryManagementService.updateFrozenQuantity(materialALLInventoryManagementReqList);
        return ResultBody.success();
    }

    @PostMapping("/removeFrozenQuantity")
    @ApiOperation(value = "原材料移出冻结数量", response = ResultBody.class)
    public ResultBody removeFrozenQuantity(@RequestBody List<MaterialALLInventoryManagementReq> materialALLInventoryManagementReqList) {
        materialInventoryManagementService.removeFrozenQuantity(materialALLInventoryManagementReqList);
        return ResultBody.success();
    }
}
